package com.zplus.managers.petservices;

import com.zplus.managers.petservices.controllers.AdminController;
import com.zplus.managers.petservices.entities.EntityBase;
import com.zplus.managers.petservices.entities.User;
import com.zplus.managers.petservices.services.UserService;
import com.zplus.managers.petservices.utils.Message;
import com.zplus.managers.petservices.utils.Page;
import com.zplus.managers.petservices.utils.PagingFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;
/**
 * Unit Test For Paging System
 */
@SpringBootTest
public class PagingDemos
{
    @Autowired
    UserService us;

    PagingFactory pf;
    List<User> users;
    @BeforeEach
    void setUp()
    {
        users = us.queryAllUser();
        pf = new PagingFactory(users, 2);
    }

    @Test
    void getPageOne()
    {
        boolean isPass = true;
        // set answer
        String ans[] = {"张三丰","黄仁勋"};
        // begin paging
        Page page = pf.nextPage();
        Map<String, ? extends EntityBase> pageContent = page.getPageContent();
        Set<String> strings = pageContent.keySet();
        int i = 0;
        if(pageContent.isEmpty())
            isPass = false;

        for (String id : strings)
        {
            User t = (User) pageContent.get(id);
            if (!t.getNickName().equals(ans[i]))
                isPass = false;
            i++;
        }
        assertTrue(isPass);
    }

    @Test
    void getPageByIndex()
    {
        int index = 3;
        Page page = pf.getPage(index);
        Map<String, ? extends EntityBase> content = page.getPageContent();
        assertEquals(((User)content.get("5")).getNickName(), "熊广");
    }

}
